package com.zyinhao.common;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created by 27529 on 2018/12/8.
 */
@Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
public abstract class BaseServiceImpl<M extends BaseMapper<T>, T extends BaseEntity> extends ServiceImpl<M, T> implements BaseService<T> {

    /*================  其他相关操作：获取Mapper  ================*/
    protected BaseMapper<T> getBaseMapper(){
        return baseMapper;
    }

    /*================  保存操作  ================*/
    @Override
    public void saveObject(T t) {
        if(null == t){ return; }
        this.getBaseMapper().insert(t);
    }

    @Override
    public void saveOrUpdateObject(T t) {
        if(null == t){ return; }
        if(null != t.getId()){
            this.updateObject(t);
        }else{
            this.saveObject(t);
        }
    }

    @Override
    public void saveBatchObjects(Collection<T> entities) {
        if(null == entities || entities.size()<=0 ){ return; }
        //TODO 待实现：通过mapper来实现
//        this.getBaseMapper().saveBatchObjects(entities);
    }

    /*================  删除操作  ================*/
    @Override
    public int deleteObject(String id) {
        if(null == id){ return 0; }
        return this.getBaseMapper().deleteById(id);
    }

    @Override
    public int deleteObjectByIds(String ids) {
        if(null == ids || ids.length()<=0 ){ return 0; }
        //TODO 待实现：通过mapper来实现
//        return this.getBaseMapper().deleteBatchIds(ids);
        return 0;
    }

    @Override
    public int deleteObjectByIds(Collection<String> idList) {
        if(null == idList || idList.size()<=0 ){ return 0; }
        return this.getBaseMapper().deleteBatchIds(idList);
    }

    @Override
    public int deleteObjectsByProperty(Map<String, Object> maps) {
        if(null == maps){ return 0; }
        return this.getBaseMapper().deleteByMap(maps);
    }

    /*================  修改操作  ================*/
    @Override
    public void updateObject(T t) {
        if(null == t || null == t.getId()){ return; }
        this.getBaseMapper().updateById(t);
    }

    @Override
    public void updateBatchObjects(Collection<T> entities) {
        if(null == entities){ return; }
//        this.getBaseMapper().updateBatchObjects(entities);
        //TODO 待实现：通过mapper来实现
    }

    @Override
    public void updateObjectsByProperty(Map<String, Object> maps) {
        if(null == maps){ return; }
//        this.getBaseMapper().updateObjectsByProperty(maps);
    }

    /*================  查询操作  ================*/
    @Override
    public T findObject(String id) {
        if(null == id){ return null; }
        return this.getBaseMapper().selectById(id);
    }

    @Override
    public List<T> findObjectsByIds(String ids) {
        if(null == ids || ids.length()<=0 ){ return null; }
        return this.findObjectsByIds(Arrays.asList(ids.split(",")));
    }

    @Override
    public List<T> findObjectsByIds(Collection<String> idList) {
        if(null == idList){ return new ArrayList<T>(); }
        return this.getBaseMapper().selectBatchIds(idList);
    }
}
